﻿package com.badibadi.adapter;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.badibadi.infos.Users;
import com.badibadi.uniclubber.R;
import com.view.my_view.MyCheckBox;

import android.content.Context;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class InformAdapter extends BaseAdapter {

	// private String[] Inform_Type;
	private List<Users> list;
	private Context context;
	private Map<Integer, String> map;

	private String data = null;

	public InformAdapter(Context ct, List<Users> list) {
		// TODO Auto-generated constructor stub
		context = ct;
		// Inform_Type = context.getResources()
		// .getStringArray(R.array.Inform_type);
		map = new HashMap<Integer, String>();
		this.list = list;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(final int position, View convertView,
			final ViewGroup viewGroup) {
		// TODO Auto-generated method stub

		LinearLayout ll = new LinearLayout(context);

		MyCheckBox checkbox = new MyCheckBox(context);
		final TextView tv = new TextView(context);
		tv.setText(list.get(position).getName());
		// checkbox.setTextAppearance(context, R.style.CustomCheckboxTheme);

		checkbox.setId(position);
		ll.setGravity(Gravity.CENTER_VERTICAL);
		ll.addView(checkbox);
		ll.addView(tv);
		checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				// TODO Auto-generated method stub
				// 如果选中，数据是空的话
				data = list.get(position).getId();
				if (isChecked && data == "") {
					buttonView.setBackgroundResource(R.drawable.check_true);
					map.put(position, tv.getText().toString().trim());
					// data = tv.getText().toString();
					// 如果选中，并且数据不为空 这里在迭代器中将以前的重置
				} else if (isChecked && data != "") {
					Iterator it = map.entrySet().iterator();
					while (it.hasNext()) {
						Map.Entry entry = (Map.Entry) it.next();
						int key = (Integer) entry.getKey();
						String value = (String) entry.getValue();
						//    
						CheckBox cc = (CheckBox) viewGroup.findViewById(key);
						cc.setBackgroundResource(R.drawable.check_false);
						map.remove(key);
					}
					// data = null;
					// data = tv.getText().toString();
					map.put(position, tv.getText().toString().trim());
				}
//				   ("数据是:" + data);
				//    
				// Toast.makeText(context,"你点击了" + isChecked +
				// Inform_Type[position],
				// Toast.LENGTH_SHORT).show();

			}
		});

		return ll;
	}

	public Map<Integer, String> getMap() {
		return map;
	}

	public void setMap(Map<Integer, String> map) {
		this.map = map;
	}

	public String getData() {
		return data;
	}

	public void setData(String data) {
		this.data = data;
	}

}
